﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MinistryStandardPlatformBusiness.DBService;
using MinistryStandardPlatformBusiness.MessagePool.Entity;

namespace MinistryStandardPlatformBusiness.Helper
{
  public  class DBHelper
    {
        public static DBService.DBHelperClient GlobalDBWCFHelper = new DBService.DBHelperClient();

        public static string testDBWCF()
        {
           return GlobalDBWCFHelper.BexcuteSql("admin", "SELECT *   FROM [MinistryStandardPlatform].[dbo].[LoginUsers]");
        }
      //基础数据库
        public static string getReportDataByProcHelper(string userid, string procName)
        {
            string strRe = "error";
            string flags = "ok";
            using (DBService.DBHelperClient client = new DBHelperClient())
            {
                try
                {
                    strRe = client.BexecuteProc(userid,procName);
                    flags = strRe.Equals("error") ? strRe : "ok";
                    Log.WriteLog("Report", flags + "|" + userid + "|" + procName);
                }
                catch (Exception)
                {
                    Log.WriteLog("Report", "error|" + userid + "|" + procName);
                }              
            }
            return strRe;
        }

        public static string getReportDataByProcNoParaHelper(string userid, string procName)
        {
            string strRe = "error";
            string flags = "ok";
            using (DBService.DBHelperClient client = new DBHelperClient())
            {
                try
                {
                    strRe = client.BexecuteProcNoPara(userid, procName);
                    flags = strRe.Equals("error") ? strRe : "ok";
                    Log.WriteLog("Report", flags + "|" + userid + "|" + procName);
                }
                catch (Exception)
                {
                    Log.WriteLog("Report",  "error|" + userid + "|" + procName);
                }
            }
            return strRe;
        }

        public static string getReportDataBySqlHelper(string userid, string sql)
        {
            string strRe = "error";
            string flags = "ok";
            using (DBService.DBHelperClient client = new DBHelperClient())
            {
                try
                {
                    strRe = client.BexcuteSql(userid, sql);
                    flags = strRe.Equals("error") ? strRe : "ok";
                    Log.WriteLog("Report", flags + "|" + userid + "|" + sql);
                }
                catch (Exception)
                {
                    Log.WriteLog("Report",  "error|" + userid + "|" + sql);
                }                
            }

            return strRe;
        }

        public static string getReportFromGeoBase(string userid, string sql, string strStartDate, string strEndDate,string isGeo)
        {
            string rs = "error";

            System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy-MM-dd HH:mm:sss";
            try
            {
                DateTime dts = Convert.ToDateTime(strStartDate, dtFormat);//"2014-06-23 13:33"
                DateTime dte = Convert.ToDateTime(strEndDate, dtFormat);//"2014-06-24 15:33"
                using (DBService.DBHelperClient client = new DBHelperClient())
                {
                    rs = client.YExecuteReportSql(userid, dts, dte, sql, isGeo);

                }
                Log.WriteLog("getHistoryDetails", userid + "|" + sql + "|" + strStartDate + "|" + strEndDate);
            }
            catch (Exception e)
            {
                Log.WriteLog("getHistoryError", userid + "|" + sql + "|" + strStartDate + "|" + strEndDate + e.Message);
            }
            return rs;
        }
        public static string getReportFromGeoBase(string userid, string strStartDate, string strEndDate,string sim)
        {
            string rs = "error";

            System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy-MM-dd HH:mm:sss";
            try
            {
                DateTime dts = Convert.ToDateTime(strStartDate, dtFormat);//"2014-06-23 13:33"
                DateTime dte = Convert.ToDateTime(strEndDate, dtFormat);//"2014-06-24 15:33"
                using (DBService.DBHelperClient client = new DBHelperClient())
                {
                    rs = client.YExecuteHis(userid, dts, dte, sim);
                }

                Log.WriteLog("getHistoryDetails", userid +  "|" + strStartDate + "|" + strEndDate);
            }
            catch (Exception)
            {
                Log.WriteLog("getHistoryError", userid + "|"  + strStartDate + "|" + strEndDate);
            }
            return rs;
        }

        public static string getCheckDbExitsOrNot(string dateTime)
        {
            string reStr = "0";
            System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy-MM-dd HH:mm:sss";
            try
            {
                DateTime dts = Convert.ToDateTime(dateTime, dtFormat);//"2014-06-23 13:33"
                using (DBService.DBHelperClient client = new DBHelperClient())
                {
                    reStr = client.YExecuteDBExits(dts);
                }
                if (!reStr.ToLower().Contains("error"))
                    reStr = "1";
                else
                    reStr = "0";
                Log.WriteLog("getCheckDbExitsOrNot", dateTime);
            }
            catch (Exception)
            {
                Log.WriteLog("getCheckDbExitsOrNotError", dateTime);
            }
            return reStr;

        }

    }
     
}
